/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import beans.ObraSocialBean;
import daos.Dao;
import daos.DaoFactory;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import wso.CartillaProfesionalesSerializable;

/**
 *
 * @author C
 */
public class MostrarListaNovedadesAction extends Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion = request.getSession(true);
        Class serviceClass, portClass;
        Object service, port;
        Method metGetPort, metGetCartilla;       
        String os = (String) sesion.getAttribute("nom_os");
        
        try {     
            Dao dao = DaoFactory.getDao("ObraSocial");             
            ObraSocialBean osBean=  (ObraSocialBean) dao.get(new ObraSocialBean(os));       
            
            serviceClass = Class.forName("wso."+osBean.getAccesoWS()+"_Service");
            portClass = Class.forName("wso." +osBean.getAccesoWS());
            service = serviceClass.newInstance();     
                  
            metGetPort    = serviceClass.getMethod("get"+osBean.getAccesoWS()+"Port", (Class[]) null);                       
            port   = (Object) metGetPort.invoke(service, null);            
            metGetCartilla = portClass.getMethod("returnCartilla", (Class[]) null);           
            List <CartillaProfesionalesSerializable> lista = (List <CartillaProfesionalesSerializable>) metGetCartilla.invoke(port, null);
                         
            request.setAttribute("cartilla", lista);   
        } catch (Exception ex) { 
            request.setAttribute("Error", ex.getMessage());
            System.out.println("Error    " + ex.getMessage());
        }
        finally {
            this.gotoPage("/cartillaprofesionales.jsp", request, response);
        }    
    }    
}
